package com.xywy.bloodandroid.vollery;

import java.io.File;

import android.app.ActivityManager;
import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
import com.xywy.bloodandroid.ASApplication;
import com.xywy.bloodandroid.util.FileUtil;

/**
 * 璐熻矗鎵ц璇锋眰鍜屽浘鐗囧姞杞� * 
 * @author dance
 * 
 */
public class VolleyManager {
	//  鍙栬繍琛屽唴瀛橀槞鍊肩殑1/8浣滀负鍥剧墖缂撳瓨
	public static final int MEM_CACHE_SIZE = 1024 * 1024 * ((ActivityManager)ASApplication.application.getSystemService(Context.ACTIVITY_SERVICE))
			.getMemoryClass() / 8;
	// 鍥剧墖纭洏缂撳瓨澶у皬
	private static final int DISK_CACHE_SIZE = 100 * 1024 * 1024;// 100M

	private static DiskBasedCache diskImageCache = new DiskBasedCache(
			FileUtil.getCacheDir(ASApplication.application),
			DISK_CACHE_SIZE);

	private static RequestQueue mRequestQueue;
//	private static ImageLoader mImageLoader;

	/**
	 * init RequestQuest,should be called when Application create;
	 */
	public static void init() {
		mRequestQueue = new RequestQueue(diskImageCache, new BasicNetwork(
				new HurlStack()));
//		mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(
//				MEM_CACHE_SIZE));
		mRequestQueue.start();
	}

	/**
	 * execute a request
	 * 
	 * @param request
	 * @param tag
	 */
	public static void addRequest(Request<?> request, Object tag) {
		if (tag != null) {
			request.setTag(tag);
		}
		mRequestQueue.add(request);
	}

	/**
	 * cancle a request,when current view is destory
	 * 
	 * @param tag
	 */
	public static void cancelRequest(Object tag) {
		mRequestQueue.cancelAll(tag);
	}

	public static File getCachedImageFile(String url) {
		return diskImageCache.getFileForKey(url);
	}

}
